<?php

ob_start();

class OrdersController extends AppController {

    var $name = 'Orders';
    var $helpers = array('Html', 'Ajax', 'Javascript', 'Form', 'Session');
    var $uses = array('Order', 'Customer', 'Paymethod','Product','Orderdetail');

    function index($id=null) {
        $this->layout = "home";
        $this->Order->id = $id;
        $this->set('orderread', $this->Order->read());
        $this->set('allorder', $this->Order->find('all'));
        $this->set('customers', $this->Customer->find('all'));
        $this->set('paymethods', $this->Paymethod->find('all'));
    }

    function save($id = null) {
        $id = $_REQUEST['id'];
        $customer_id = 1;
        $order_date = date("Y-m-d");
        $paymethod_id = $_REQUEST['paymethod_id'];
        $description = $_REQUEST['description'];
        $status = 1;
        if (($id != null) && ($id != "")) {
            $this->Order->id = $id;
            $this->Order->set('custommer_id', $customer_id);
            $this->Order->set('date', $order_date);
            $this->Order->set('paymethod_id', $paymethod_id);
            $this->Order->set('status', $status);
            $this->Order->set('description', $description);
            $this->Order->save();
        } else {
            $this->Order->create();
            $this->Order->set('customer_id', $customer_id);
            $this->Order->set('date', $order_date);
            $this->Order->set('paymethod_id', $paymethod_id);
            $this->Order->set('status', $status);
            $this->Order->set('description', $description);
            $this->Order->saveAll();
            
            $lastid = $this->Order->getLastInsertID();
            
            
            $price = 0;
            foreach($_SESSION["cart"] as $pid => $qty){                                
                
                $gia = $this->Product->find("all",array("conditions" => array("Product.id" => $pid)));
                
                foreach($gia as $g){
                    $price = $g["Product"]["price"];
                }                
                $this->Orderdetail->create();
                $this->Orderdetail->set('order_id', $lastid);
                $this->Orderdetail->set('product_id', $pid);
                $this->Orderdetail->set('value',$price );
                $this->Orderdetail->set('quantity', $qty);                
                $this->Orderdetail->saveAll();
            }
            
        }
        $this->redirect(array("action" => "index"));
    }
    function saveorder(){
        $id = $_REQUEST['id'];
        $customer_id = 1;
        $order_date = date("Y-m-d");
        $paymethod_id = $_REQUEST['paymethod'];
        $description = $_REQUEST['description'];
		$status = 1;
        
        $this->Order->create();
            $this->Order->set('customer_id', $customer_id);
            $this->Order->set('date', $order_date);
            $this->Order->set('paymethod_id', $paymethod_id);
            $this->Order->set('status', $status);
            $this->Order->set('description', $description);
            $this->Order->saveAll();
            
            $lastid = $this->Order->getLastInsertID();
            
            
            $price = 0;
            foreach($_SESSION["cart"] as $pid => $qty){                                
                
                $gia = $this->Product->find("all",array("conditions" => array("Product.id" => $pid)));
                
                foreach($gia as $g){
                    $price = $g["Product"]["price"];
                }                
                $this->Orderdetail->create();
                $this->Orderdetail->set('order_id', $lastid);
                $this->Orderdetail->set('product_id', $pid);
                $this->Orderdetail->set('value',$price );
                $this->Orderdetail->set('quantity', $qty);                
                $this->Orderdetail->saveAll();
            }
			$this->autoRender= false;
			unset($_SESSION["cart"]);
            $this->redirect("/frontends");
    }
    function delete() {
        $id = $_REQUEST['id'];
        if ($id != null && $id != "") {
            $this->Order->delete($id);
            $this->Session->setFlash(__('ban co chac xoa san pham khong', true));
            $this->redirect(array("action" => "index"));
        }
    }
	function view($id=null)
	{
	 $this->layout = "home";
	 $id = $_REQUEST['id'];
	 $this->Order->id= $id;
	 $this->set('oders',$this->Order->read());
	 $this->set('orderdetail',$this->Orderdetail->find('all'));
	 $this->set('customersi', $this->Customer->find('all'));
	 $this->set('productsi', $this->Product->find('all'));
	}

}

?>